package cn.btks.commonserver.util.poi;

import com.deepoove.poi.data.CellRenderData;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.style.TableStyle;
import com.deepoove.poi.policy.DynamicTableRenderPolicy;
import com.deepoove.poi.policy.MiniTableRenderPolicy;
import com.deepoove.poi.util.TableTools;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.poi.xwpf.usermodel.TableRowAlign;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;

import java.util.List;

public class DynamicMergeTablePolicy extends DynamicTableRenderPolicy {

    //合并行索引
    private List<Integer> mergeIndexs;
    // 填充数据所在行
    private int startRowIndex = 1;
    // 每行单元格数量
    private int cellNumber = 0;
    // 需要合并列索引
    private int mergeColumn = 0;
    // 合并列的起始行索引
    private int fromRow = 0;
    //合并列的截止行索引
    private int toRow = 0;
    // 序号
    int number = 1;

    public DynamicMergeTablePolicy(int startRowIndex, int cellNumber, int mergeColumn, int fromRow, int toRow,
                                   Object mergeIndexs) {
        this.startRowIndex = startRowIndex;
        this.cellNumber = cellNumber;
        this.mergeColumn = mergeColumn;
        this.fromRow = fromRow;
        this.toRow = toRow;
        this.mergeIndexs = (List<Integer>) mergeIndexs;
    }

    @Override
    public void render(XWPFTable table, Object data) {
        if (null == data) return;
        TableStyle rowStyle = new TableStyle();
        rowStyle.setAlign(STJc.CENTER);
        List<RowRenderData> detailData = (List<RowRenderData>) data;

        List<RowRenderData> measures = detailData;
        if (null != measures) {
            table.removeRow(startRowIndex);
            for (int i = 0; i < measures.size(); i++) {
                measures.get(i).setRowStyle(rowStyle);
                XWPFTableRow insertNewTableRow = table.insertNewTableRow(startRowIndex);
                for (int j = 0; j < cellNumber; j++) insertNewTableRow.createCell();
                List<CellRenderData> cells = measures.get(i).getCells();
                copytTableRow(insertNewTableRow, table.getRow(1));
                cells.get(0).getCellText().setText(String.valueOf(number));

                MiniTableRenderPolicy.Helper.renderRow(table, startRowIndex, measures.get(i));
                number++;
                startRowIndex++;
            }
            if (this.mergeIndexs != null && this.mergeIndexs.size() > 0) {
                for(int i = 0;i< this.mergeIndexs.size();i++){
                    TableTools.mergeCellsVertically(table, mergeColumn, fromRow, toRow);
                    if (i <this.mergeIndexs.size() -1){
                        fromRow = toRow + 1;
                        toRow += this.mergeIndexs.get(i + 1);
                    }
                }
            }
        }
    }

    private void copytTableRow(XWPFTableRow target, XWPFTableRow source) {
        // 复制样式
        target.getCtRow().setTrPr(source.getCtRow().getTrPr());
        // 复制单元格
        for (int i = 0; i < target.getTableCells().size(); i++) {
            copyTableCell(target.getCell(i), source.getCell(5));//pos:5为单独的单元格样式
        }
    }

    private void copyTableCell(XWPFTableCell target, XWPFTableCell source) {
        // 列属性
        target.getCTTc().setTcPr(source.getCTTc().getTcPr());
        // 删除目标 targetCell 所有单元格
        for (int pos = 0; pos < target.getParagraphs().size(); pos++) {
            target.removeParagraph(pos);
        }
        // 添加段落
        for (XWPFParagraph sp : source.getParagraphs()) {
            XWPFParagraph targetP = target.addParagraph();
            copyParagraph(targetP, sp);
        }
    }

    private void copyParagraph(XWPFParagraph target, XWPFParagraph source) {
        // 设置段落样式
        target.getCTP().setPPr(source.getCTP().getPPr());
        // 添加Run标签
        for (int pos = 0; pos < target.getRuns().size(); pos++) {
            target.removeRun(pos);
        }
    }
}